<?php
if( !defined('IN_INDEX') )
{
	die('This page is hidden.');
}
if(!checkSession())
{
	function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?=$url?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=login");
	exit;
}
else
{
	if(isset($_REQUEST['id']))
	{
		$USER_PROFILE = new User('id', $_REQUEST['id']);
		$page_url = "index.php?action=profile&id=" . $_REQUEST['id'] . "&sub=stats";
	}
	else
	{
		$USER_PROFILE = $USER;
		$page_url = "index.php?action=profile&sub=stats";
	}
}
include('inc/functions_blog.php');
//include('Charts/Includes/FusionCharts.php');

$blogs = getTrainingList(" WHERE user_id='" . $USER_PROFILE->getId() . "'");

$dateformat = $CONFIG['date_format'];
$dateformatlong = $CONFIG['date_format_long'];
$dateformatweek = $CONFIG['date_format_week'];
$dateformatmonth = $CONFIG['date_format_month'];
$dateformatyear = $CONFIG['date_format_year'];

$time_now = time();
$blog['today'] = date('d', $time_now);
$blog['tomonth'] = date('m', $time_now);
$blog['toyear'] = date('Y', $time_now);

$blog['fromday'] = date('d', $time_now);
$blog['frommonth'] = date('m', $time_now);
$blog['fromyear'] = date('Y', $time_now);

if(isset($_POST['select']))
{
	$blog['fromday']=$_POST['fromday'];
	$blog['frommonth']=$_POST['frommonth'];
	$blog['fromyear']=$_POST['fromyear'];
	$fromdate = mktime(date('H', $time_now), date('i', $time_now), date('s', $time_now), $blog['frommonth'], $blog['fromday'], $blog['fromyear']);
	
	$blog['today']=$_POST['today'];
	$blog['tomonth']=$_POST['tomonth'];
	$blog['toyear']=$_POST['toyear'];	
}
?>
<!--
<SCRIPT LANGUAGE="Javascript" SRC="Charts/FusionCharts.js"></SCRIPT>
-->

<script type="text/javascript" src="./Highcharts/js/highcharts.js"></script>

<div id='postpath'>
		<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
		 &gt; <a href="index.php?action=blogs" title='<?php echo translate('showallblogs'); ?>'><?php echo translate('blog'); ?></a> 
         &gt; <a href="index.php?action=profile&id=<?php echo $USER_PROFILE->getId(); ?>"><?php echo ucf($USER_PROFILE->getFirstname()) . " " . ucf($USER_PROFILE->getLastname()); ?></a>
		&gt; <a href="index.php?action=profile&sub=stats&id=<?php echo $USER_PROFILE->getId(); ?>" title='<?php echo translate('stats'); ?>'><?php echo translate('stats'); ?></a>
        &gt; <a href="index.php?action=profile&sub=stats_volume&id=<?php echo $USER_PROFILE->getId(); ?>" title='<?php echo translate('volume'); ?>'><?php echo translate('volume'); ?></a>
</div>
<?php 

if(count($blogs) > 0)
{
?>
<br />
<h2><?php echo translate('volume'); ?></h2>
<br />
<?php echo translate('volume_subtext'); ?>
<br /><br />
<form method="post" action="index.php?action=profile&sub=stats_volume&id=<?php echo $USER_PROFILE->getId(); ?>"> 
	<table border="0">
    <tr>
    <td>
    <b><?php echo ucf(translate('exercise')); ?></b>
    <select name="exercise">
    <option name="exercise" value="exercise_id='150' OR exercise_id='100' OR exercise_id='101' OR exercise_id='102' OR exercise_id='121' OR exercise_id='65' OR exercise_id='35' OR exercise_id='36' OR exercise_id='110' OR exercise_id='115' OR exercise_id='116' OR exercise_id='62' OR exercise_id='137' OR exercise_id='1' OR exercise_id='134' OR exercise_id='33' OR exercise_id='5' OR exercise_id='7' OR exercise_id='184'" <?php if(isset($_POST['select']) && stripslashes($_POST['exercise']) == "exercise_id='150' OR exercise_id='100' OR exercise_id='101' OR exercise_id='102' OR exercise_id='121' OR exercise_id='65' OR exercise_id='35' OR exercise_id='36' OR exercise_id='110' OR exercise_id='115' OR exercise_id='116' OR exercise_id='62' OR exercise_id='137' OR exercise_id='1' OR exercise_id='134' OR exercise_id='33' OR exercise_id='5' OR exercise_id='7' OR exercise_id='184'") { echo "selected='selected'"; } ?>><?php echo translate('Squat'); ?></option>
    
    <option name="exercise" value="exercise_id='133' OR exercise_id='132' OR exercise_id='131' OR exercise_id='130' OR exercise_id='129' OR exercise_id='128' OR exercise_id='127' OR exercise_id='125' OR exercise_id='124' OR exercise_id='119' OR exercise_id='88' OR exercise_id='104' OR exercise_id='113' OR exercise_id='143' OR exercise_id='38' OR exercise_id='26' OR exercise_id='27' OR exercise_id='89' OR exercise_id='114' OR exercise_id='29' OR exercise_id='152' OR exercise_id='32' OR exercise_id='34' OR exercise_id='19' OR exercise_id='6' OR exercise_id='37' OR exercise_id='2' OR exercise_id='180' OR exercise_id='179' OR exercise_id='181' OR exercise_id='190'" <?php if(isset($_POST['select']) && stripslashes($_POST['exercise']) == "exercise_id='133' OR exercise_id='132' OR exercise_id='131' OR exercise_id='130' OR exercise_id='129' OR exercise_id='128' OR exercise_id='127' OR exercise_id='125' OR exercise_id='124' OR exercise_id='119' OR exercise_id='88' OR exercise_id='104' OR exercise_id='113' OR exercise_id='143' OR exercise_id='38' OR exercise_id='26' OR exercise_id='27' OR exercise_id='89' OR exercise_id='114' OR exercise_id='29' OR exercise_id='152' OR exercise_id='32' OR exercise_id='34' OR exercise_id='19' OR exercise_id='6' OR exercise_id='37' OR exercise_id='2' OR exercise_id='180' OR exercise_id='179' OR exercise_id='181' OR exercise_id='190'") { echo "selected='selected'"; } ?>><?php echo translate('Bench press'); ?></option>
    
    <option name="exercise" value="exercise_id='106' OR exercise_id='158' OR exercise_id='117' OR exercise_id='112' OR exercise_id='44' OR exercise_id='107' OR exercise_id='120' OR exercise_id='122' OR exercise_id='144' OR exercise_id='147' OR exercise_id='146' OR exercise_id='135' OR exercise_id='153' OR exercise_id='136' OR exercise_id='126' OR exercise_id='145' OR exercise_id='123' OR exercise_id='118' OR exercise_id='41' OR exercise_id='3' OR exercise_id='8' OR exercise_id='39' OR exercise_id='40' OR exercise_id='31' OR exercise_id='30' OR exercise_id='20'" <?php if(isset($_POST['select']) && stripslashes($_POST['exercise']) == "exercise_id='106' OR exercise_id='158' OR exercise_id='117' OR exercise_id='112' OR exercise_id='44' OR exercise_id='107' OR exercise_id='120' OR exercise_id='122' OR exercise_id='144' OR exercise_id='147' OR exercise_id='146' OR exercise_id='135' OR exercise_id='153' OR exercise_id='136' OR exercise_id='126' OR exercise_id='145' OR exercise_id='123' OR exercise_id='118' OR exercise_id='41' OR exercise_id='3' OR exercise_id='8' OR exercise_id='39' OR exercise_id='40' OR exercise_id='31' OR exercise_id='30' OR exercise_id='20'") { echo "selected='selected'"; } ?>><?php echo translate('Deadlift'); ?></option>
    </select>
    
    <b><?php echo translate('from'); ?></b>	<select id="day" name="fromday">
            <?php
            for($i=1; $i<=31; $i++)
            {
                if($i == $blog['fromday'])
                {
                    echo "<option value=\"" . $i . "\" selected=\"selected\">" . $i . "</option>\n";
                }
                else
                {
                    echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
                }
            }
            ?>
        </select>
        
        <select id="month" name="frommonth">
            <?php
            foreach($MONTHS as $k => $v)
            {
                if(($k+1) == $blog['frommonth'])
                {
                    echo "<option value=\"" . ($k+1) . "\" selected=\"selected\">" . ucfirst(translate($v)) . "</option>\n";
                }
                else
                {
                    echo "<option value=\"" . ($k+1) . "\">" . ucfirst(translate($v)) . "</option>\n";
                }
            }
            ?>
        </select>
    
        <select id="year" name="fromyear">
            <?php
            for($i=date('Y', $time_now); $i>=(date('Y', $time_now) - 11); $i--)
            {
                if($i == $blog['fromyear'])
                {
                    echo "<option value=\"" . $i . "\" selected=\"selected\">" . $i . "</option>\n";
                }
                else
                {
                    echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
                }
            }
            ?>
        </select> 
    </td>
        <td>
        <b><?php echo translate('to'); ?></b>
        <select id="day" name="today">
        <?php
        for($i=1; $i<=31; $i++)
        {
            if($i == $blog['today'])
            {
                echo "<option value=\"" . $i . "\" selected=\"selected\">" . $i . "</option>\n";
            }
            else
            {
                echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
            }
        }
        ?>
    	</select>
        
        <select id="month" name="tomonth">
            <?php
            foreach($MONTHS as $k => $v)
            {
                if(($k+1) == $blog['tomonth'])
                {
                    echo "<option value=\"" . ($k+1) . "\" selected=\"selected\">" . ucfirst(translate($v)) . "</option>\n";
                }
                else
                {
                    echo "<option value=\"" . ($k+1) . "\">" . ucfirst(translate($v)) . "</option>\n";
                }
            }
            ?>
        </select>
        
        <select id="year" name="toyear">
            <?php
            for($i=date('Y', $time_now); $i>=(date('Y', $time_now) - 11); $i--)
            {
                if($i == $blog['toyear'])
                {
                    echo "<option value=\"" . $i . "\" selected=\"selected\">" . $i . "</option>\n";
                }
                else
                {
                    echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
                }
            }
            ?>
        </select> 
        <b>Vis pr:</b>
        <select id="period" name="period">
	        <option name="period" value="1" <?php if(isset($_POST['select']) && $_POST['period']== 1) { echo " selected='selected'"; } ?>><?php echo ucf(translate('day')); ?></option>
	        <option name="period" value="2" <?php if(isset($_POST['select']) && $_POST['period']== 2) { echo " selected='selected'"; } ?>><?php echo ucf(translate('week')); ?></option>
	        <option name="period" value="3" <?php if(isset($_POST['select']) && $_POST['period']== 3) { echo " selected='selected'"; } ?>><?php echo ucf(translate('month')); ?></option>
	        <option name="period" value="4" <?php if(isset($_POST['select']) && $_POST['period']== 4) { echo " selected='selected'"; } ?>><?php echo ucf(translate('year')); ?></option>
       		<option name="period" value="5" <?php if(isset($_POST['select']) && $_POST['period']== 5) { echo " selected='selected'"; } ?>><?php echo ucf(translate('period')); ?></option>
       
	    </select>    
        <td>
        <input type="hidden" name="user_id" value="<?php echo $USER_PROFILE->getId(); ?>"  />
        <input type="submit" style="font-weight:bold;" name="select" value=" <?php echo ucf(translate('show')); ?> "  />
        </td>
    </table>
</form>

<div id="container_graph" style="width: auto; height: auto; margin: 0 auto"></div>

<?php 
if(isset($_POST['select']))
{
	$blog['fromday']=$_POST['fromday'];
	$blog['frommonth']=$_POST['frommonth'];
	$blog['fromyear']=$_POST['fromyear'];
	$fromdate = mktime(date('H', 00), date('i', 00), date('s', 00), $blog['frommonth'], $blog['fromday'], $blog['fromyear']);
	$fromdateyear = mktime(date('H', 00), date('i', 00), date('s', 00), 1, 1, $blog['fromyear']);
	
	$blog['today']=$_POST['today'];
	$blog['tomonth']=$_POST['tomonth'];
	$blog['toyear']=$_POST['toyear'];
	$todate = mktime(date('H', $time_now), date('i', $time_now), date('s', $time_now), $blog['tomonth'], $blog['today'], $blog['toyear']);
	$todateyear = mktime(date('H', 00), date('i', 00), date('s', 00), 12, 31, $blog['toyear']);
	
	$period = $_POST['period'];
	$user_id = $_POST['user_id'];
	$exercises = stripslashes($_POST['exercise']);

	echo "<br />";
	
	if($period == 1)
	{
	// Get all the data
	$result = mysql_query("SELECT DISTINCT
	training.date, 
	training.training_id,
	training.place,
	training.bodyweight
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND training.user_id =". $user_id ." AND training.date > " . $fromdate . " AND training.date < " . $todate . "
	ORDER BY training.date") or die(mysql_error());

	$num = mysql_num_rows($result);
	$i=1;

	if($i < $num)
	{ 		
		$sumkg = 0;
		$i = 0;
		$date_array = array();	
		$weight_array = array();
		$min_max_array = array();
		$mean_array = array();
		$max_array = array();
		$array_url = array();
		
		while($row = mysql_fetch_array($result)){       
			
			$kg = total_kg_lifted2($user_id, $row['training_id'], $exercises);
			
			if($kg > 0)
			{
				   if($w_metric == "kg"){ 
					  $date_array[] = formatDate($row['date'], $dateformat); 
					  $weight_array[] = $kg;
				   }else{ 
					  $date_array[] = formatDate($row['date'], $dateformat);  	
					  $weight_array[] = r_nr($kg);
				   }
				  $i++;
				  $tmp = get_min_max_lifted($user_id, $row['training_id'], $exercises);
				  $max_array[] = $tmp['max'];
				  $mean_array[] =  $tmp['mean'];
				  $min_max_array[] = $tmp;		
				  $array_url[] = "index.php?action=profile&sub=show&tid=". $row['training_id'] ;
				  			
			}
			$sumkg += $kg;
		} 
			
		$averagekg = $sumkg/$i;
		
		foreach($date_array as &$val){
			$val = "'".$val."'";
		}
		$cat = "[". implode(',',$date_array) ."]";
		$weight = "[". implode(',',$weight_array) ."]";
		$max_weight = "[". implode(',',$max_array) ."]";
		$mean_weight = "[". implode(',',$mean_array) ."]";
		$json_min_max = json_encode($min_max_array);
		$json_url = json_encode($array_url);
		
		$min_of_MAXW = min($max_array);
		$max_of_MAXW = max($max_array);
		$min_of_MEANW = min($mean_array);
		$max_of_MEANW = max($mean_array);
		$min_of_W = min($weight_array);
		$max_of_W = ceil( max($weight_array) );
		//will be multiply mean_weight and max_weight with MCONT
		$MCONT = 10;
		if(count($date_array) >= 20){
			foreach($max_array as &$val){
				$val = $MCONT* $val;
			}
			foreach($mean_array as &$val){
				$val = $MCONT* $val;
			}
			$max_weight = "[". implode(',',$max_array) ."]";
			$mean_weight = "[". implode(',',$mean_array) ."]";
		}
?>		
		<script type="text/javascript" >
			var json = eval(<?php echo $json_min_max; ?>);
			var json_url = eval(<?php echo $json_url; ?>);
			var min_title = '<?php echo translate("min_weight");?>';
			var max_title = '<?php echo translate("max_weight");?>';
			var kg_title = '<?php echo translate("Kg");?>';
			var num_rows = <?php echo count($date_array);?>;
			var DCONT = <?php echo $MCONT;?>;
			
			var chart;
		<?php if(count($date_array) < 20) :?>	
			$(document).ready(function() {
			 	chart = new Highcharts.Chart({
					chart: {
						renderTo: 'container_graph',
						zoomType: 'xy'
					},
					title: {
						text: '<?php echo ucf(translate('volume')). " " . translate('i'). " " . strtolower(translate('exercise')) ;?>'
					},
					subtitle: {
						text: '<?php echo ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId());?>'
					},
					xAxis: {
						categories: eval(<?php echo $cat;?>), 
						title: {
							text: '<?php echo translate("day_title");?>'
						},
						labels: {
							rotation: 90,
							y: 40
						}
					},
					yAxis:[ {
						min:<?php echo $min_of_MEANW;?>,
						max:<?php echo $max_of_MAXW+1;?>,
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#89A54E'
							}
						},
						title: {
							text: '<?php echo translate("max_weight");?>',
							style: {
								color: '#89A54E'
							}
						},
						opposite: true
					},
					{
						max: <?php echo $max_of_W?>,
						min: <?php echo $min_of_W?>,
						gridLineWidth: 0,
						title: {
							text: '<?php echo translate("total_day");?>',
							style: {
								color: '#4572A7'
							}
						},
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#4572A7'
							}
						}
					},
					{
						min:<?php echo $min_of_MEANW;?>,
						max:<?php echo $max_of_MAXW+1;?>,
						gridLineWidth: 0,
						title: {
							text: '<?php echo translate("mean_weight");?>',
							style: {
								color: '#AA4643'
							}
						},
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#AA4643'
							}
						},
						opposite: true
					} ],
					tooltip: {
						formatter: function() {
							var unit = {
								'<?php echo translate("max_weight");?>': kg_title,
								'<?php echo translate("total_day");?>': kg_title,
								'<?php echo translate("mean_weight");?>': kg_title
							}[this.series.name];
							
							return ''+
								this.x +': '+ this.y +' '+ unit;
						}
					},
					plotOptions: {
						series: {
							lineWidth: 2,
							dataLabels: {
								enabled: false,
								formatter: function() {
									return this.y+''+ kg_title +'<br />'+ this.x;
								},
								x: -40,
								y:0
							},
							cursor: 'pointer',
				            point: {
				                events: {
				                    click: function() {
				                        location.href = json_url[this.x];
				                    }
				                }
				            }
						}
					},
					legend: {
						layout: 'vertical',//
						align: 'left',
						x: 120,
						verticalAlign: 'top',
						y: 0,
						floating: true,
						backgroundColor: '#FFFFFF'
					},
					credits: {
						enabled: false
					},
			        series: [
					{
						name: '<?php echo translate("total_day");?>',
						color: '#4572A7',
						type: 'column',
						yAxis: 1,
						data: eval(<?php echo $weight;?>)
					},
					{
						name: '<?php echo translate("mean_weight");?>',
						type: 'spline',
						color: '#AA4643',
						yAxis: 2,
						data: eval(<?php echo $mean_weight;?>),
						marker: {
							enabled: false
						},
						dashStyle: 'shortdot'					
					}, {
						name: '<?php echo translate("max_weight");?>',
						color: '#89A54E',
						type: 'spline',
						data: eval(<?php echo $max_weight;?>)
					}
					]
				});	
			});
		<?php else: ?>
			$(document).ready(function() {
				chart = new Highcharts.Chart({
					chart: {
						renderTo: 'container_graph', 
						defaultSeriesType: 'area'
					},
					title: {
						text: '<?php echo ucf(translate('volume')). " " . translate('i'). " " . strtolower(translate('exercise')) ;?>'
					},
					subtitle: {
						text: '<?php echo ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId());?>'
					},
					xAxis: {
						categories: eval(<?php echo $cat;?>), 
						title: {
							text: '<?php echo translate("day_title");?>'
						},
						labels: {
							formatter: function() {
								return ''; // clean, unformatted number for year
							}
						}							
					},
					yAxis: {
						title: {
							text: '<?php echo translate("weight_unit");?>'
						},
						labels: {
							rotation: 0,
							y: 0
						}
					},
					tooltip: {
						formatter: function() {
							if( this.series.name == '<?php echo translate("mean_weight")?>' || this.series.name == '<?php echo translate("max_weight")?>'){
								this.y = this.y/DCONT;
							}
							return ''+
								this.x +': '+ this.y +' '+ kg_title;
						}
					},
					plotOptions: {
						area: {
							point: {
				                events: {
				                    click: function() {
				                        location.href = json_url[this.x];
				                    }
				                }
				            },
							cursor: 'pointer'
						}	
					},
					series: [{
						name: '<?php echo translate("total_day");?>',
						color: '#4572A7',
						data: eval(<?php echo $weight;?>)
					},
					{
						name: '<?php echo translate("mean_weight");?>',
						color: '#AA4643',
						data: eval(<?php echo $mean_weight;?>),
					}, 
					{
						name: '<?php echo translate("max_weight");?>',
						color: '#89A54E',
						data: eval(<?php echo $max_weight;?>)
					}]
				});	
			});
				
		<?php endif;?>	
		</script>
<?php			
		echo "<div class='fixed'> 
						</div> 
					</div> 
					</div>
				<div class='fixed'> 
				</div> 
				<div id='footer'> 
				<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a> 
					<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br /> 
				<br />
				
				<a rel='license' href='http://creativecommons.org/licenses/by-nc/3.0/'>
				<img alt='Creative Commons License' style='border-width:0' 
				src='http://i.creativecommons.org/l/by-nc/3.0/80x15.png' /></a>
				<br />This work is licensed under a <a rel='license' href='http://creativecommons.org/licenses/by-nc/3.0/'>
				Creative Commons Attribution-NonCommercial 3.0 Unported License</a>.	
					</div>	
			</div>"; 
		exit; 
	}
	else
	{
		echo translate('noresultsbw');
		echo "<div class='fixed'>
						</div>
					</div>
					</div>
				<div class='fixed'>
				</div>
				<div id='footer'>
				<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
					<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
			
					</div>";
		exit;
	}  
	}
	
	if($period == 2)
	{
	// Get all the data
	$result = mysql_query("SELECT DISTINCT
	training.date, 
	training.training_id,
	training.place,
	training.bodyweight
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND training.user_id =". $user_id ." AND training.date > " . $fromdate . " AND training.date < " . $todate . "
	ORDER BY training.date") or die(mysql_error());

	$num = mysql_num_rows($result);
	$i=1;

	if($i < $num)
	{ 		
		$sumkg = 0;
		$i = 0;
		
		$week_array = array();	
		$weight_array = array();
		$min_max_array = array();
		$array_week_key = array();
		$array_week_value = array();
		
		$mean_array = array();
		$max_array = array();
		$array_url = array();
		
		while($row = mysql_fetch_array($result)){       
			
			$kg = total_kg_lifted2($user_id, $row['training_id'], $exercises);
			
			if($kg > 0)
			{
				$tmp = get_min_max_lifted($user_id, $row['training_id'], $exercises);
				$url = "index.php?action=profile&sub=show&tid=". $row['training_id'] ;
			    if($w_metric == "kg"){ 
				 //  $strXML .= "<set label='". formatDate($row['date'], $dateformat) ."' value='" . $kg . "' />";
				   $week_format = strftime('%U/%Y',$row['date']);
				   $week_array[] = array('key'=>"$week_format",'val'=>$kg, 'mean'=>$tmp['mean'], 'max'=>$tmp['max'], 'url'=>$url );
			    }else{ 
				 //  $strXML .= "<set label='". formatDate($row['date'], $dateformat) ."' value='" . r_nr($kg) . "' />";
				   $week_format = strftime('%U/%Y',$row['date']);
				   $week_array[] = array( 'key'=>"$week_format",'val'=>r_nr($kg), 'mean'=>$tmp['mean'], 'max'=>$tmp['max'], 'url'=>$url );	
			    }
				$i++;
				$array_week_key[] = $week_format;  
			}
			$sumkg += $kg;
		} 
		$array_week_key = array_unique($array_week_key);
		
		foreach($array_week_key as $key){
			$array_week_value[$key] = 0;
			$mean_array[$key]	= 0;
			$max_array[$key]	= 0;
			$array_url[$key]	= '';
			$i = 0;$sum=0;
			foreach($week_array as $w_key=> $ar_val){
				if($key == $ar_val['key']){
					$array_week_value["$key"] += $ar_val['val'];
					if($max_array["$key"] <= $ar_val['max'] ){
						$max_array["$key"] = $ar_val['max'];
						$array_url[$key] = $ar_val['url'];
					}
					$sum += $ar_val['mean'];
					$i++;
					$mean_array[$key] = ceil($sum/$i);
				}
			}
		}
		
		foreach($array_week_key as &$val){
			$val = "'".$val."'";
		}
		 
		$cat = "[". implode(',',$array_week_key) ."]";
		$weight = "[". implode(',',$array_week_value) ."]";
		$max_weight = "[". implode(',',$max_array) ."]";
		$mean_weight = "[". implode(',',$mean_array) ."]";
		$json_url = json_encode($array_url);
		
		$min_of_MAXW = min($max_array);
		$max_of_MAXW = max($max_array);
		$min_of_MEANW = min($mean_array);
		$max_of_MEANW = max($mean_array);
		$min_of_W = min($array_week_value);
		$max_of_W = ceil( max($array_week_value)/ 5000 ) * 5000;
	?>
		
	<script type="text/javascript" >
		var json_url = eval(<?php echo $json_url; ?>);
		var kg_title = '<?php echo translate("Kg");?>';
		var num_rows = <?php echo count($array_week_value);?>
		
		var chart;
		$(document).ready(function() {
			$('#container_graph').css('height',500);
			chart = new Highcharts.Chart({
				chart: {
					renderTo: 'container_graph',
					zoomType: 'xy'
				},
				title: {
					text: '<?php echo ucf(translate('volume')). " " . translate('i'). " " . strtolower(translate('exercise')) ;?>'
				},
				subtitle: {
					text: '<?php echo ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId());?>'
				},
				xAxis: {
					categories: eval(<?php echo $cat;?>), 
					title: {
						text: '<?php echo translate("week_title");?>'
					},
					labels: {
						rotation: 90,
						y: 40
					}
				},
				yAxis:[ 
					{
						min:<?php echo $min_of_MEANW;?>,
						max:<?php echo $max_of_MAXW+1;?>,
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#89A54E'
							}
						},
						title: {
							text: '<?php echo translate("max_weight");?>',
							style: {
								color: '#89A54E'
							}
						},
						opposite: true
					},
					{
						max: <?php echo $max_of_W?>,
						min: <?php echo $min_of_W?>,
						gridLineWidth: 0,
						title: {
							text: '<?php echo translate("total_week");?>',
							style: {
								color: '#4572A7'
							}
						},
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#4572A7'
							}
						}
					},
					{
						min:<?php echo $min_of_MEANW;?>,
						max:<?php echo $max_of_MAXW+1;?>,
						gridLineWidth: 0,
						title: {
							text: '<?php echo translate("mean_weight");?>',
							style: {
								color: '#AA4643'
							}
						},
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#AA4643'
							}
						},
						opposite: true
					}
				]
				,
				tooltip: {
					formatter: function() {
						var unit = {
							'<?php echo translate("max_weight");?>': kg_title,
							'<?php echo translate("total_week");?>': kg_title,
							'<?php echo translate("mean_weight");?>': kg_title
						}[this.series.name];
						
						return ''+
							this.x +': '+ this.y +' '+ unit;
					}
				},
				plotOptions: {
					series: {
						lineWidth: 2,
						dataLabels: {
							enabled: false,
							formatter: function() {
								return this.y+''+ kg_title +'<br />'+ this.x;
							},
							x: -40,
							y:0
						},
						cursor: 'pointer',
						point: {
							events: {
								click: function() {
									location.href = json_url[this.category];
								}
							}
						}
					}
				},
				legend: {
					layout: 'vertical',
					align: 'left',
					x: 120,
					verticalAlign: 'top',
					y: 0,
					floating: true,
					backgroundColor: '#FFFFFF'
				},
				credits: {
					enabled: false
				},
		        series:  [
					{
						name: '<?php echo translate("total_week");?>',
						color: '#4572A7',
						type: 'column',
						yAxis: 1,
						data: eval(<?php echo $weight;?>)
					},
					{
						name: '<?php echo translate("mean_weight");?>',
						type: 'spline',
						color: '#AA4643',
						yAxis: 2,
						data: eval(<?php echo $mean_weight;?>),
						marker: {
							enabled: false
						},
						dashStyle: 'shortdot'					
					}, {
						name: '<?php echo translate("max_weight");?>',
						color: '#89A54E',
						type: 'spline',
						data: eval(<?php echo $max_weight;?>)
					}
				]
			});	
		});
		
	</script>
	
<?php		
	echo "<div class='fixed'> 
						</div> 
					</div> 
					</div>
				<div class='fixed'> 
				</div> 
				<div id='footer'> 
				<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a> 
					<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br /> 
				<br />
				
				<a rel='license' href='http://creativecommons.org/licenses/by-nc/3.0/'>
				<img alt='Creative Commons License' style='border-width:0' 
				src='http://i.creativecommons.org/l/by-nc/3.0/80x15.png' /></a>
				<br />This work is licensed under a <a rel='license' href='http://creativecommons.org/licenses/by-nc/3.0/'>
				Creative Commons Attribution-NonCommercial 3.0 Unported License</a>.	
					</div>	
					</div>"; 
		exit; 
	}
	else
	{
		echo translate('noresultsbw');
		echo "<div class='fixed'>
						</div>
					</div>
					</div>
				<div class='fixed'>
				</div>
				<div id='footer'>
				<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
					<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
					</div>";
		exit;
	}  
   }
   
   if($period == 3)
	{
	// Get all the data
	$result = mysql_query("SELECT DISTINCT
	training.date, 
	training.training_id,
	training.place,
	training.bodyweight
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND training.user_id =". $user_id ." AND training.date > " . $fromdate . " AND training.date < " . $todate . "
	ORDER BY training.date") or die(mysql_error());

	$num = mysql_num_rows($result);
	$i=1;
//$strXML = "<chart caption='" . ucf(translate('volume')) . " " . translate('i'). " " . strtolower(translate('exercise')) ."' subCaption='" . ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId()) . "' showBorder='1' formatNumberScale='0' numberSuffix='". $w_metric ."' baseFont='Arial' exportEnabled='1' exportAtClient='0' exportHandler='Charts/ExportHandlers/PHP/FCExporter.php' exportAction='download' exportTargetWindow='_blank' exportFileName='" . ucf(translate('volume')) . "'>";

	if($i < $num)
	{ 		
		$sumkg = 0;
		$i = 0;
		$month_array = array();	
		$weight_array = array();
		$min_max_array = array();
		$array_month_key = array();
		$array_month_value = array();
		
		$mean_array = array();
		$max_array = array();
		$array_url = array();
		
		while($row = mysql_fetch_array($result)){       
			$kg = total_kg_lifted2($user_id, $row['training_id'], $exercises);
			
			if($kg > 0)
			{
				$tmp = get_min_max_lifted($user_id, $row['training_id'], $exercises);
				$url = "index.php?action=profile&sub=show&tid=". $row['training_id'] ;
			
			   if($w_metric == "kg"){ 
				 //  $strXML .= "<set label='". formatDate($row['date'], $dateformat) ."' value='" . $kg . "' />";
				   $month_format = strftime('%b/%Y',$row['date']);
				   $month_array[] = array('key'=>"$month_format", 'val'=>$kg, 'mean'=>$tmp['mean'], 'max'=>$tmp['max'], 'url'=>$url);
			   }else{ 
				 //  $strXML .= "<set label='". formatDate($row['date'], $dateformat) ."' value='" . r_nr($kg) . "' />"; 
			   	   $month_format = strftime('%b/%Y',$row['date']);
				   $month_array[] = array('key'=>"$month_format",'val'=>r_nr($kg), 'mean'=>$tmp['mean'], 'max'=>$tmp['max'], 'url'=>$url);	
			   }
			   $i++;
			   $array_month_key[] = $month_format;  
			}
			$sumkg += $kg;
		} 
	 
		$sumkg;
		$averagekg = $sumkg/$i;
		
		$array_month_key = array_unique($array_month_key);
		
		foreach($array_month_key as $key){
			$array_month_value[$key] = 0;
			$mean_array[$key]	= 0;
			$max_array[$key]	= 0;
			$array_url[$key]	= '';
			$i = 0;$sum=0;
			
			foreach($month_array as $m_key=> $ar_val){
				if($key == $ar_val['key']){
					$array_month_value["$key"] += $ar_val['val'];
					if($max_array["$key"] <= $ar_val['max'] ){
						$max_array["$key"] = $ar_val['max'];
						$array_url[$key] = $ar_val['url'];
					}
					$sum += $ar_val['mean'];
					$i++;
					$mean_array[$key] = ceil($sum/$i);
				}
			}
		}
		
		foreach($array_month_key as &$val){
			$val = "'".$val."'";
		}
		 
		$cat = "[". implode(',',$array_month_key) ."]";
		$weight = "[". implode(',',$array_month_value) ."]";
		$max_weight = "[". implode(',',$max_array) ."]";
		$mean_weight = "[". implode(',',$mean_array) ."]";
		$json_url = json_encode($array_url);
		
		$min_of_MAXW = min($max_array);
		$max_of_MAXW = max($max_array);
		$min_of_MEANW = min($mean_array);
		$max_of_MEANW = max($mean_array);
		$min_of_W = min($array_month_value);
		$max_of_W = ceil( max($array_month_value) );
	?>
		
	<script type="text/javascript" >
		var json_url = eval(<?php echo $json_url; ?>);
		var kg_title = '<?php echo translate("Kg");?>';
		var num_rows = <?php echo count($array_month_value);?>
		
		var chart;
		$(document).ready(function() {
			chart = new Highcharts.Chart({
				chart: {
					renderTo: 'container_graph',
					zoomType: 'xy'
				},
				title: {
					text: '<?php echo ucf(translate('volume')). " " . translate('i'). " " . strtolower(translate('exercise')) ;?>'
				},
				subtitle: {
					text: '<?php echo ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId());?>'
				},
				xAxis: {
					categories: eval(<?php echo $cat;?>), 
					title: {
						text: '<?php echo translate("month_title");?>'
					},
					labels: {
						rotation: 90,
						y: 40
					}
				},
				yAxis: [ 
					{
						min:<?php echo $min_of_MEANW;?>,
						max:<?php echo $max_of_MAXW+1;?>,
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#89A54E'
							}
						},
						title: {
							text: '<?php echo translate("max_weight");?>',
							style: {
								color: '#89A54E'
							}
						},
						opposite: true
					},
					{
						max: <?php echo $max_of_W?>,
						min: <?php echo $min_of_W?>,
						gridLineWidth: 0,
						title: {
							text: '<?php echo translate("total_month");?>',
							style: {
								color: '#4572A7'
							}
						},
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#4572A7'
							}
						}
					},
					{
						min:<?php echo $min_of_MEANW;?>,
						max:<?php echo $max_of_MAXW+1;?>,
						gridLineWidth: 0,
						title: {
							text: '<?php echo translate("mean_weight");?>',
							style: {
								color: '#AA4643'
							}
						},
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#AA4643'
							}
						},
						opposite: true
					}
				],
				tooltip: {
					formatter: function() {
						var unit = {
							'<?php echo translate("max_weight");?>': kg_title,
							'<?php echo translate("total_month");?>': kg_title,
							'<?php echo translate("mean_weight");?>': kg_title
						}[this.series.name];
						
						return ''+
							this.x +': '+ this.y +' '+ unit;
					}
				},
				plotOptions: {
					series: {
						lineWidth: 2,
						dataLabels: {
							enabled: false,
							formatter: function() {
								return this.y+''+ kg_title +'<br />'+ this.x;
							},
							x: -40,
							y:0
						},
						cursor: 'pointer',
						point: {
							events: {
								click: function() {
									location.href = json_url[this.category];
								}
							}
						}
					}
				},
				legend: {
					layout: 'vertical',
					align: 'left',
					x: 120,
					verticalAlign: 'top',
					y: 0,
					floating: true,
					backgroundColor: '#FFFFFF'
				},
				credits: {
					enabled: false
				},
		        series:  [
					{
						name: '<?php echo translate("total_month");?>',
						color: '#4572A7',
						type: 'column',
						yAxis: 1,
						data: eval(<?php echo $weight;?>)
					},
					{
						name: '<?php echo translate("mean_weight");?>',
						type: 'spline',
						color: '#AA4643',
						yAxis: 2,
						data: eval(<?php echo $mean_weight;?>),
						marker: {
							enabled: false
						},
						dashStyle: 'shortdot'					
					}, {
						name: '<?php echo translate("max_weight");?>',
						color: '#89A54E',
						type: 'spline',
						data: eval(<?php echo $max_weight;?>)
					}
				]
			});	
		});
		
	</script>	
		
	<?php			
		
		echo "<div class='fixed'> 
							</div> 
						</div> 
						</div>
					<div class='fixed'> 
					</div> 
					<div id='footer'> 
					<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a> 
						<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br /> 
					<br />
				
				<a rel='license' href='http://creativecommons.org/licenses/by-nc/3.0/'>
				<img alt='Creative Commons License' style='border-width:0' 
				src='http://i.creativecommons.org/l/by-nc/3.0/80x15.png' /></a>
				<br />This work is licensed under a <a rel='license' href='http://creativecommons.org/licenses/by-nc/3.0/'>
				Creative Commons Attribution-NonCommercial 3.0 Unported License</a>.	
					</div>	
						</div>"; 
			exit; 
	 
	}
	else
	{
		echo translate('noresultsbw');
		echo "<div class='fixed'>
						</div>
					</div>
					</div>
				<div class='fixed'>
				</div>
				<div id='footer'>
				<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
					<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
					</div>";
		exit;
	}  
   }
   
 if($period == 5)
 {
	// Get all the data
	$result = mysql_query("SELECT DISTINCT
	training.date, 
	training.training_id,
	training.place,
	training.bodyweight
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND training.user_id =". $user_id ." AND training.date > " . $fromdate . " AND training.date < " . $todate . "
	ORDER BY training.date") or die(mysql_error());

	$num = mysql_num_rows($result);
	$i=1;
//$strXML = "<chart caption='" . ucf(translate('volume')) . " " . translate('i'). " " . strtolower(translate('exercise')) ."' subCaption='" . ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId()) . "' showBorder='1' formatNumberScale='0' numberSuffix='". $w_metric ."' baseFont='Arial' exportEnabled='1' exportAtClient='0' exportHandler='Charts/ExportHandlers/PHP/FCExporter.php' exportAction='download' exportTargetWindow='_blank' exportFileName='" . ucf(translate('volume')) . "'>";

	if($i < $num)
	{ 		
		$sumkg = 0;
		$i = 0;
		
		$year_array = array();	
		$weight_array = array();
		$min_max_array = array();
		$array_year_key = array();
		$array_year_value = array();	
		
		$mean_array = array();
		$max_array = array();
		$array_url = array();	
		while($row = mysql_fetch_array($result))
		{       
			$kg = total_kg_lifted_year($user_id, $row['training_id'], $exercises);
			
			$sumkg += $kg;
			
			if($kg > 0)
			{
			   $tmp = get_min_max_lifted($user_id, $row['training_id'], $exercises);
			   $url = "index.php?action=profile&sub=show&tid=". $row['training_id'] ;	
			   if($w_metric == "kg"){ 
				   //$strXML .= "<set label='". formatDate($row['date'], $dateformatyear) ."' value='" . $kg . "' />";
				   $year_format = strftime('%Y',$row['date']);
				   $year_array[] = array('key'=>"$year_format",'val'=>$kg, 'mean'=>$tmp['mean'], 'max'=>$tmp['max'], 'url'=>$url );
			   }else{ 
				   //$strXML .= "<set label='". formatDate($row['date'], $dateformatyear) ."' value='" . r_nr($kg) . "' />"; 
			   	   $year_format = strftime('%Y',$row['date']);
				   $year_array[] = array('key'=>"$year_format",'val'=>r_nr($kg), 'mean'=>$tmp['mean'], 'max'=>$tmp['max'], 'url'=>$url );	
			   }
			   $i++;
			   $array_year_key[] = $year_format;  
			}
				
		} 
		$i;
		$sumkg;
		$averagekg = $sumkg/$i;
		
		//$array_year_key = array_unique(array(0));
		
		$key = "period";
		$array_year_value[$key] = 0;
		$mean_array[$key]	= 0;
		$max_array[$key]	= 0;
		$array_url[$key]	= '';
		$i = 0;$sum=0;
		
		foreach($year_array as $y_key=> $ar_val){
			//if($key == $ar_val['key']){
				$array_year_value["$key"] += $ar_val['val'];
				if( $max_array["$key"] <= $ar_val['max'] ){
					$max_array["$key"] = $ar_val['max'];
					$array_url["$key"] = $ar_val['url'];
				}
				$sum += $ar_val['mean'];
				$i++;
				$mean_array[$key] = ceil($sum/$i);
			//}
		}
		 
		
		//foreach($array_year_key as &$val){
		//	$val = "'".$val."'";
		//}
		$title_prd = translate("period")." ". date('M jS, Y',$fromdate)." ".translate("to")." ".date('M jS, Y',$todate);
			
		$cat = "['". $key ."']";
		$weight = "[". implode(',',$array_year_value) ."]";
		$max_weight = "[". implode(',',$max_array) ."]";
		$mean_weight = "[". implode(',',$mean_array) ."]";
		$json_url = json_encode($array_url);
		
		$min_of_MAXW = min($max_array);
		$max_of_MAXW = max($max_array);
		$min_of_MEANW = abs( min($mean_array) );
		$max_of_MEANW = max($mean_array);
		$min_of_W = min($array_year_value);
		$max_of_W = ceil( max($array_year_value) );
?>
	<script type="text/javascript" >
		var json_url = eval(<?php echo $json_url; ?>);
		var kg_title = '<?php echo translate("Kg");?>';
	//	var num_rows = <?php echo count($array_year_value);?>
		
		var chart;
		$(document).ready(function() {
			  
			chart = new Highcharts.Chart({
				chart: {
					renderTo: 'container_graph',
					zoomType: 'xy'
				},
				title: {
					text: '<?php echo ucf(translate('volume')). " " . translate('i'). " " . strtolower(translate('exercise')) ;?>'
				},
				subtitle: {
					text: '<?php echo ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId());?>'
				},
				xAxis: {
					categories: eval(<?php echo $cat;?>), 
					title: {
						text: '<?php echo $title_prd;?>'
					},
					labels: {
						rotation: 0,
						y: 10,
						enabled:false
					}
				},
				yAxis: [ 
					{
						min:<?php echo $min_of_MEANW;?>,
						max:<?php echo $max_of_MAXW+1;?>,
					//	gridLineWidth: 2,
					//	lineWidth:2,
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#89A54E'
							}
						},
						title: {
							text: '<?php echo translate("max_weight");?>',
							style: {
								color: '#89A54E'
							}
						},
						opposite: true
					},
					{
					//	max: <?php echo $max_of_W?>,
					//	min: <?php echo $min_of_W?>,
					//	gridLineWidth: 0,
						title: {
							text: '<?php echo translate("total_period");?>',
							style: {
								color: '#4572A7'
							}
						},
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#4572A7'
							}
						}
					},
					{
						min:<?php echo $min_of_MEANW;?>,
						max:<?php echo $max_of_MAXW+1;?>,
					//	gridLineWidth: 2,
					//	lineWidth:2,
						 gridLineDashStyle: 'longdash',
						title: {
							text: '<?php echo translate("mean_weight");?>',
							style: {
								color: '#AA4643'
							}
						},
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#AA4643'
							}
						},
						opposite: true
					}
				],
				tooltip: {
					formatter: function() {
						var unit = {
							'<?php echo translate("max_weight");?>': kg_title,
							'<?php echo translate("total_period");?>': kg_title,
							'<?php echo translate("mean_weight");?>': kg_title
						}[this.series.name];
						
						return ''+
							 this.y +' '+ unit;
					}
				},
				plotOptions: {
					series: {
						lineWidth: 2,
						dataLabels: {
							enabled: true,
							formatter: function() {
								return this.y+''+ kg_title;
							},
							x: -40,
							y:0
						},
						cursor: 'pointer',
						point: {
							events: {
								click: function() {
									location.href = json_url[this.category];
								}
							}
						}
					}
				},
				legend: {
					layout: 'vertical',
					align: 'left',
					x: 120,
					verticalAlign: 'top',
					y: 0,
					floating: true,
					backgroundColor: '#FFFFFF'
				},
				credits: {
					enabled: false
				},
		        series:  [
					{
						name: '<?php echo translate("total_period");?>',
						color: '#4572A7',
						type: 'column',
						yAxis: 1,
						data: eval(<?php echo $weight;?>)
					},
					{
						name: '<?php echo translate("mean_weight");?>',
						type: 'spline',
						color: '#AA4643',
					//	yAxis: 0,
						data: eval(<?php echo $mean_weight;?>),
						marker: {
							enabled: true
						}	//,
					//	dashStyle: 'shortdot'					
					}, {
						name: '<?php echo translate("max_weight");?>',
						color: '#89A54E',
						type: 'spline',
						data: eval(<?php echo $max_weight;?>)
					}
				]
			});	
		});
		
	</script>

<?php		
	echo "<div class='fixed'> 
						</div> 
					</div> 
					</div>
				<div class='fixed'> 
				</div> 
				<div id='footer'> 
				<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a> 
					<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br /> 
				<br />
				
				<a rel='license' href='http://creativecommons.org/licenses/by-nc/3.0/'>
				<img alt='Creative Commons License' style='border-width:0' 
				src='http://i.creativecommons.org/l/by-nc/3.0/80x15.png' /></a>
				<br />This work is licensed under a <a rel='license' href='http://creativecommons.org/licenses/by-nc/3.0/'>
				Creative Commons Attribution-NonCommercial 3.0 Unported License</a>.	
					</div>	
					</div>"; 
		exit; 
	 
	}
	else
	{
		echo translate('noresultsbw');
		echo "<div class='fixed'>
						</div>
					</div>
					</div>
				<div class='fixed'>
				</div>
				<div id='footer'>
				<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
					<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
					</div>";
		exit;
	}  
	}
if($period == 4){
   // Get all the data
    $fromdate = 0;$todate= 9999999999;
	$result = mysql_query("SELECT DISTINCT
	training.date, 
	training.training_id,
	training.place,
	training.bodyweight
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND training.user_id =". $user_id ." AND training.date > " . $fromdate . " AND training.date < " . $todate . "
	ORDER BY training.date") or die(mysql_error());

	$num = mysql_num_rows($result);
	$i=1;

	if($i < $num)
	{ 		
		$sumkg = 0;
		$i = 0;
		
		$prd_array = array();	
		$weight_array = array();
		$weight_max_array = array();
		$array_prd_date = array();
		$array_prd_value = array();	
		$array_year_key = array();
		
		$mean_array = array();
		$max_array = array();
		$array_url = array();
		
		while($row = mysql_fetch_array($result))
		{       
			$kg = total_kg_lifted_year($user_id, $row['training_id'], $exercises);
			
			$sumkg += $kg;
			if($kg > 0)
			{
				$tmp = get_min_max_lifted($user_id, $row['training_id'], $exercises);
				$url = "index.php?action=profile&sub=show&tid=". $row['training_id'] ;
				
				$year_format = strftime('%Y',$row['date']);
				$array_year_key[] = $year_format;  
				   if($w_metric == "kg"){ 
					 // $prd_array[] = formatDate($row['date'], $dateformat); 
					  $weight_array[] =  array('date'=>formatDate($row['date'], $dateformat),'key'=>"$year_format", 'val'=>$kg, 'mean'=>$tmp['mean'], 'max'=>$tmp['max'], 'url'=>$url);
				   }else{ 
					//  $prd_array[] = formatDate($row['date'], $dateformat);  	
					  $weight_array[] =  array('date'=>formatDate($row['date'], $dateformat),'key'=>"$year_format", 'val'=>r_nr($kg), 'mean'=>$tmp['mean'], 'max'=>$tmp['max'], 'url'=>$url);
				   }
				  $i++;
			}
		} 
	/***	
		if(count($weight_array) > 20){
			$segment = ceil(count($weight_array)/15);
			$array_chunk = array_chunk($weight_array, $segment);
			$j = 0;
			foreach($array_chunk as $period){
				$array_prd_date[$j] = "'".$period[0]['date']."'";
				$array_prd_value[$j] = 0;
				$mean_array[$j]	= 0;
				$max_array[$j]	= 0;
				$array_url[$j]	= '';
				$ii = 0;$sum=0;
				foreach($period as $one){
					$array_prd_value[$j] += $one['val'];
					if($max_array[$j] <= $one['max'] ){
						$max_array[$j] = $one['max'];
						$array_url[$j] = $one['url'];
					}
					$sum += $one['mean'];
					$ii++;
					$mean_array[$j] = ceil($sum/$ii);
				}
				$j++;
			}
			 
			
		}else{
			foreach($weight_array as $key){
				$array_prd_date[] = "'".$key['date']."'";
				$array_prd_value[] = $key['val'];	
				$max_array[] = $key['max'];
				$array_url[] = $key['url'];
				$mean_array[] = $key['mean'];
			}
		}
		$cat = "[". implode(',',$array_prd_date) ."]";
		$weight = "[". implode(',',$array_prd_value) ."]";
		$max_weight = "[". implode(',',$max_array) ."]";
		$mean_weight = "[". implode(',',$mean_array) ."]";
		$json_url = json_encode($array_url);
	**/
		$array_year_key = array_unique($array_year_key);
		
		foreach($array_year_key as $key){
			$array_prd_value[$key] = 0;
			$mean_array[$key]	= 0;
			$max_array[$key]	= 0;
			$array_url[$key]	= '';
			$i = 0;$sum=0;
			
			foreach($weight_array as $y_key=> $ar_val){
				if($key == $ar_val['key']){
					$array_prd_value["$key"] += $ar_val['val'];
					if($max_array["$key"] <= $ar_val['max'] ){
						$max_array["$key"] = $ar_val['max'];
						$array_url["$key"] = $ar_val['url'];
					}
					$sum += $ar_val['mean'];
					$i++;
					$mean_array[$key] = ceil($sum/$i);
				}
			}
		}
		
		foreach($array_year_key as &$val){
			$val = "'".$val."'";
		}
		//small trick for make mean weight graph is not too tall than other, multiply with 100000 for its value are very big
		//foreach($mean_array as &$val){
		//	$val = 100000*$val;
		//}
		
		$cat = "[". implode(',',$array_year_key) ."]";
		$weight = "[". implode(',',$array_prd_value) ."]";
		$max_weight = "[". implode(',',$max_array) ."]";
		$mean_weight = "[". implode(',',$mean_array) ."]";
		$json_url = json_encode($array_url);
		
		$min_of_MAXW = min($max_array);
		$max_of_MAXW = max($max_array);
		$min_of_MEANW = min($mean_array);
		$max_of_MEANW = max($mean_array);
		$min_of_W = min($array_prd_value);
		$max_of_W = ceil( max($array_prd_value) );
?>
	<script type="text/javascript" >
		var json_url = eval(<?php echo $json_url; ?>);
		var kg_title = '<?php echo translate("Kg");?>';
		
		var chart;
		$(document).ready(function() {
			$('#container_graph').css('height',800);
			chart = new Highcharts.Chart({
				chart: {
					renderTo: 'container_graph',
					zoomType: 'xy'
				},
				title: {
					text: '<?php echo ucf(translate('volume')). " " . translate('i'). " " . strtolower(translate('exercise')) ;?>'
				},
				subtitle: {
					text: '<?php echo ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId());?>'
				},
				xAxis: {
					categories: eval(<?php echo $cat;?>), 
					title: {
						text: '<?php echo translate("year_title");?>'
					}
					,
					labels: {
						rotation: 90,
						y: 40
					}
				},
				yAxis: [
					{
						min:<?php echo $min_of_MEANW;?>,
						max:<?php echo $max_of_MAXW+1;?>,
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#89A54E'
							}
						},
						title: {
							text: '<?php echo translate("max_weight");?>',//
							style: {
								color: '#89A54E'
							}
						},
						opposite: true
					},
					{
						max: <?php echo $max_of_W?>,
						min: <?php echo $min_of_W?>,
						gridLineWidth: 0,
						title: {
							text: '<?php echo translate("total_year");?>',
							style: {
								color: '#4572A7'
							}
						},
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#4572A7'
							}
						}
					}
					,
					{
						min:<?php echo $min_of_MEANW;?>,
						max:<?php echo $max_of_MAXW+1;?>,
						gridLineWidth: 0,
						title: {
							text: '<?php echo translate("mean_weight");?>',//
							style: {
								color: '#AA4643'
							}
						},
						labels: {
							formatter: function() {
								return this.value +' '+kg_title;
							},
							style: {
								color: '#AA4643'
							}
						},
						opposite: true
					}
				],
				tooltip: {
					formatter: function() {
						var unit = {
							'<?php echo translate("max_weight");?>': kg_title,
							'<?php echo translate("total_period");?>': kg_title,
							'<?php echo translate("mean_weight");?>': kg_title
						}[this.series.name];
						
						return ''+
							this.x +': '+ this.y +' '+ unit;
					}
				},
				plotOptions: {
					series: {
						lineWidth: 2,
						dataLabels: {
							enabled: false,
							formatter: function() {
								return this.y+''+ kg_title +'<br />'+ this.x;
							},
							x: -40,
							y:0
						},
						cursor: 'pointer',
						point: {
							events: {
								click: function() {
									location.href = json_url[this.category];
								}
							}
						}
					}
				},
				legend: {
					layout: 'vertical',
					align: 'left',
					x: 120,
					verticalAlign: 'top',
					y: 0,
					floating: true,
					backgroundColor: '#FFFFFF'
				},
				credits: {
					enabled: false
				},
		        series: [
					{
						name: '<?php echo translate("total_period");?>',
						color: '#4572A7',
						type: 'column',
						yAxis: 1,
						data: eval(<?php echo $weight;?>)
					},
					 {
						name: '<?php echo translate("max_weight");?>',
						color: '#89A54E',
						type: 'spline',
						data: eval(<?php echo $max_weight;?>)
					},
					{
						name: '<?php echo translate("mean_weight");?>',
						type: 'spline',
						color: '#AA4643',
						yAxis: 2,
						data: eval(<?php echo $mean_weight;?>),
						marker: {
							enabled: false
						},
						dashStyle: 'shortdot'					
					}
				]
			});	
		});
		
	</script>

<?php		
	echo "<div class='fixed'> 
						</div> 
					</div> 
					</div>
				<div class='fixed'> 
				</div> 
				<div id='footer'> 
				<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a> 
					<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br /> 
				<br />
				
				<a rel='license' href='http://creativecommons.org/licenses/by-nc/3.0/'>
				<img alt='Creative Commons License' style='border-width:0' 
				src='http://i.creativecommons.org/l/by-nc/3.0/80x15.png' /></a>
				<br />This work is licensed under a <a rel='license' href='http://creativecommons.org/licenses/by-nc/3.0/'>
				Creative Commons Attribution-NonCommercial 3.0 Unported License</a>.	
					</div>"; 
		exit; 
	 
	}
	else
	{
		echo translate('noresultsbw');
		echo "<div class='fixed'>
						</div>
					</div>
					</div>
				<div class='fixed'>
				</div>
				<div id='footer'>
				<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
					<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
					</div>";
		exit;
	}  
	}
}
	 
}
else
{
  echo "<br /><br /><br /><br /><br /><br /><br /><br /><br />
		<center><h2>" . translate('addanewtrainingtostart') . "</h2></center>";
}
?>

